<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_actions_clearDocumentCacheAction extends f_core_Action
{
	public function execute()
	{
        $baseDir = WEBAPP_DIR.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'documents';
        if(is_dir($baseDir))
        {
            $folders = glob($baseDir.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR);
            if(count($folders) > 0)
            {
               foreach($folders as $folder)
               {
                    $this->delTree($folder);
               }
            }
        }
	}	

    private function delTree($dir)
    {
        $files = glob( $dir . '*', GLOB_MARK );
        foreach( $files as $file )
        {
            if( substr( $file, -1 ) == '/' )
                $this->delTree( $file );
            else
                unlink( $file );
        }

        if (is_dir($dir)) rmdir( $dir );
    }

	public function validate()
	{
		return true;
	}
}